<?php
namespace app\admin\controller;
use think\Db;
class Adminrole extends Base{
    public function adminrole_list(){
        $list=Db::name('adminrole')->order('id desc')->paginate(10);
        $lists=Db::name('adminrole')->select();
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function adminrole_add(){
        if(request()->isGet()){
            $ruleo=Db::name('adminruleo')->order('id asc')->select();
            for ($i=0; $i <count($ruleo) ; $i++) { 
                $ruleo[$i]['two']=Db::name('adminrulet')->where('ruleo_id',$ruleo[$i]['id'])->order('id asc')->select();
            }
            $this->assign('ruleo',$ruleo);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['one']=implode(',', $param['idos']);
            // $data['two']=implode(',', $param['idts']);
            $data['status']=1;
            $res=Db::name('adminrole')->insert($data);
            return json(['code' => 200,'msg' => '成功']);
        }
    }
    public function adminrole_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $user=Db::name('adminrole')->where('id',$param['id'])->find();
            $ones=explode(',',$user['one']);
            $twos=explode(',',$user['two']);
            $ruleo=Db::name('adminruleo')->order('id asc')->select();
            for ($i=0; $i <count($ruleo) ; $i++) { 
                $ruleo[$i]['two']=Db::name('adminrulet')->where('ruleo_id',$ruleo[$i]['id'])->order('id asc')->select();
            }
            $this->assign('ones',$ones);
            $this->assign('twos',$twos);
            $this->assign('ruleo',$ruleo);
            $this->assign('user',$user);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['one']=implode(',', $param['idos']);
            // $data['two']=implode(',', $param['idts']);
            $data['status']=1;
            $res= Db::name('adminrole')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200,'msg' => '更新成功']);
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('adminrole')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('adminrole')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function adminrole_del(){
        $param = input('post.');
        $res= Db::name('adminrole')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function adminrole_dels(){
        $param = input('post.');
        $res= Db::name('adminrole')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
